﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.Common;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.BusinessObjects;

namespace SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar
{
    public class ucMonthContent : ItemsControl
    {
        #region Variables
        private Grid grdMonthLegend = null;
        private ucContent Owner = null;
        private ScrollViewer svContent = null;
        #endregion

        #region Event Variables
        public event EventHandler Week_Clicked;
        #endregion

        #region Constructor
        public ucMonthContent(ucContent Owner)
        {
            DefaultStyleKey = typeof(ucMonthContent);
            this.Owner = Owner;
        }
        #endregion        

        #region Mehtods
        #region OnApplyTemplate
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            svContent = (ScrollViewer)base.GetTemplateChild("svContent");

            if (svContent == null) return;

            ScrollViewerExtensions.SetIsMouseWheelScrollingEnabled(svContent, true);

            grdMonthLegend = (Grid)base.GetTemplateChild("grdMonthLegend");

            if (grdMonthLegend == null) return;

            PopulateMonthLegend();
        }
        #endregion

        #region PopulateMonthLegend
        private void PopulateMonthLegend()
        {
            if (Owner != null)
            {
                DateTime tempDate = new DateTime(Owner.CurrentDate.Year, Owner.CurrentDate.Month, 1);
                System.DayOfWeek _FirstDayOfMonth = tempDate.DayOfWeek;
                int _DaysInMonth = DateTime.DaysInMonth(Owner.CurrentDate.Year, Owner.CurrentDate.Month);

                double _Width = (Owner.ActualWidth - 50) / 7;
                int _RowCount = 0;
                int _RowsInMonth = 5;

                if (_DaysInMonth == 30 && _FirstDayOfMonth >= System.DayOfWeek.Friday)
                    _RowsInMonth++;

                if (_DaysInMonth == 31 && _FirstDayOfMonth >= System.DayOfWeek.Thursday)
                    _RowsInMonth++;

                for (int i = 0; i < _RowsInMonth; i++)
                {
                    RowDefinition rdm1 = new RowDefinition();
                    rdm1.Height = new GridLength(_Width);

                    grdMonthLegend.RowDefinitions.Add(rdm1);
                }

                for (int _DayValue = 1; _DayValue <= _DaysInMonth; _DayValue++)
                {
                    tempDate = new DateTime(Owner.CurrentDate.Year, Owner.CurrentDate.Month, _DayValue);

                    if (tempDate.DayOfWeek == System.DayOfWeek.Sunday || tempDate.Day == 1)
                    {
                        Canvas cnv = new Canvas();
                        cnv.Background = Owner.MonthLegendBackground;
                        cnv.RenderTransformOrigin = new Point(0.5, 0.5);
                        cnv.Width = _Width - 20;
                        cnv.Height = 15;
                        cnv.Margin = new Thickness(-33, 18, 0, 0);

                        RotateTransform rt = new RotateTransform();
                        rt.Angle = -90;
                        cnv.RenderTransform = rt;

                        Border brd = new Border();
                        brd.BorderBrush = Owner.MonthLegendBorderBrush;
                        brd.BorderThickness = Owner.MonthLegendBorderThickness;
                        brd.Cursor = Cursors.Hand;
                        brd.MouseLeftButtonUp += new MouseButtonEventHandler(brd_MouseLeftButtonUp);
                        brd.Tag = tempDate;

                        TextBlock tbWeekSpan = new TextBlock();

                        if (tempDate.Day == 1)
                        {
                            int WeekEndDay = 6 - (int)tempDate.DayOfWeek;
                            tbWeekSpan.Text = SLControlLibrary.Resources.Common.Utility.FormatDate(SLControlLibrary.Resources.Common.Enumerations.DateFormat.ShortMonth, tempDate) + " " + tempDate.Day + " - " + tempDate.AddDays(WeekEndDay).Day;
                        }
                        else
                            tbWeekSpan.Text = SLControlLibrary.Resources.Common.Utility.FormatDate(SLControlLibrary.Resources.Common.Enumerations.DateFormat.ShortMonth, tempDate) + " " + tempDate.Day + " - " + tempDate.AddDays(6).Day;

                        tbWeekSpan.HorizontalAlignment = HorizontalAlignment.Center;
                        tbWeekSpan.VerticalAlignment = VerticalAlignment.Bottom;
                        tbWeekSpan.TextAlignment = TextAlignment.Center;
                        tbWeekSpan.Width = _Width - 20;
                        tbWeekSpan.Foreground = Owner.MonthLegendForeground;

                        brd.Tag = tempDate;
                        brd.Child = tbWeekSpan;

                        cnv.Children.Add(brd);

                        Grid.SetRow(cnv, _RowCount);

                        grdMonthLegend.Children.Add(cnv);

                        _RowCount++;
                    }
                }
            }
        }
        #endregion
        #endregion

        #region Events
        #region brd_MouseLeftButtonUp
        private void brd_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (Week_Clicked != null)
                Week_Clicked((sender as Border).Tag, null);
        }
        #endregion
        #endregion
    }
}
